package com.whu.waterworks.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.AlertRecordDAO;
import com.whu.waterworks.model.Alert;
import com.whu.waterworks.model.AlertRecord;
import com.whu.waterworks.model.AlertRecordId;

public class AlertRecordDAOImpl implements AlertRecordDAO {
	private HibernateTemplate hibernateTemplate;

	@Override
	public void save(AlertRecord entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(AlertRecord entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public String update(AlertRecord entity) {
		hibernateTemplate.saveOrUpdate(entity);
		return "success";
	}

	@Override
	public AlertRecord findById(AlertRecordId id) {
		AlertRecord alertRecord=new AlertRecord();
		String sql="from AlertRecord _AlertRecord where _AlertRecord.id.alertId="+id.getAlertId()+" and _AlertRecord.id.time='"+id.getTime()+"'";
		List<AlertRecord> alertRecords=new ArrayList<>();
		try {
			alertRecords = (ArrayList<AlertRecord>)hibernateTemplate.find(sql);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		
		if (alertRecords==null) {
			return null;
		}
		else {
			alertRecord=alertRecords.get(0);
			return alertRecord;
		}
	}

	@Override
	public List<AlertRecord> findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AlertRecord> findByAlertType(Object alertType) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AlertRecord> findByUsername(Object username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AlertRecord> findByDisposealertname(Object disposealertname) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AlertRecord> findByStationnum(Object stationnum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AlertRecord> findAll() {
		List<AlertRecord> alertRecords = (List<AlertRecord>)hibernateTemplate.find("from AlertRecord _AlertRecord");
		if (alertRecords==null) {
			return null;
		}
		else {
			return alertRecords;
		}
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
